[Linux]06 使用shell脚本发送邮件

前置条件、使用脚本发送普通邮件、带表格告警的邮件

Posted by 李玉坤 on 2018-03-09

前置条件

要使用脚本发送邮件,必须设置发送邮箱开启IMAP/SMTP服务。否则将会报错。

同时脚本所在服务器需要有sendEmail命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#下载安装包
wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
#创建目录
mkdir -p /usr/local/bin
#解压
tar zxf sendEmail-v1.56.tar.gz -C /usr/src/
#进入解压目录
cd /usr/src/sendEmail-v1.56/
#复制程序到指定目录
cp -a sendEmail /usr/local/bin/
#给执行权限
chmod +x /usr/local/bin/sendEmail
#安装组件
yum install perl-Net-SSLeay perl-IO-Socket-SSL -y

使用脚本发送测试邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash

#收件箱
EMAIL_RECIVER="li__yukun@163.com"
#发送者邮箱
EMAIL_SENDER=2504413761@qq.com
#邮箱用户名
EMAIL_USERNAME=2504413761
#邮箱密码
#使用qq邮箱进行发送需要注意:首先需要开启:POP3/SMTP服务,其次发送邮件的密码需要使用在开启POP3/SMTP服务时候腾讯提供的第三方客户端登陆码。
EMAIL_PASSWORD=vxddqmqvrxhqebdb

#附件路径
FILE1_PATH="/root/anaconda-ks.cfg"

#smtp服务器地址
EMAIL_SMTPHOST=smtp.qq.com

EMAIL_TITLE="测试"
EMAIL_CONTENT="谢谢!"

sendEmail -f ${EMAIL_SENDER} -t ${EMAIL_RECIVER} -s ${EMAIL_SMTPHOST} -u ${EMAIL_TITLE} -xu ${EMAIL_USERNAME} -xp ${EMAIL_PASSWORD} -m ${EMAIL_CONTENT} -a ${FILE1_PATH} -o message-charset=utf-8
1
2
[root@hadoop ~]# ./test.sh 
Apr 12 14:49:44 hadoop sendEmail[11425]: Email was sent successfully!

执行成功查看结果


带表格告警的邮件

准备数据/root/test.txt
如果cpu大于80那么就标红

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
[root@hadoop ~]# cat test.txt 
192.168.232.8 90 95 10
192.168.232.9 20 40 40
192.168.232.10 80 50 40
[root@hadoop ~]# pwd
/root

[root@hadoop ~]# cat test.sh
#!/bin/bash

EMAIL_RECIVER="li__yukun@163.com"
#发送者邮箱
EMAIL_SENDER=2504413761@qq.com
#邮箱用户名
EMAIL_USERNAME=2504413761
#邮箱密码
#使用qq邮箱进行发送需要注意:首先需要开启:POP3/SMTP服务,其次发送邮件的密码需要使用在开启POP3/SMTP服务时候腾讯提供的第三方客户端登陆码。
EMAIL_PASSWORD=vxddqmqvrxhqebdb

FILE1_PATH="/root/anaconda-ks.cfg"

#smtp服务器地址
EMAIL_SMTPHOST=smtp.qq.com

EMAIL_TITLE="测试"
EMAIL_CONTENT="谢谢!"


html_input(){
echo "<tr>
<td>$1</td>
<td>$2</td>
<td>$3</td>
<td>$4</td>
</tr>" >>/root/mail.html
}

html_input_red(){
echo "<tr bgcolor="#FF0000">
<td>$1</td>
<td>$2</td>
<td>$3</td>
<td>$4</td>
</tr>" >>/root/mail.html
}


set_info(){
i=1
echo "
<table border=1>
<tr>
<th>主机ip</th>
<th>cpu使用率</th>
<th>内存使用率</th>
<th>磁盘使用率</th>
</tr>" > /root/mail.html
host_list=$(awk '{print $1}' /root/test.txt) #主机信息
for html_host in $host_list
do
j=2

html_cpu=$(awk 'NR==i { print $j}' i=$i j=$j /root/test.txt) #本条对应cpu
let "j++"
html_mem=$(awk 'NR==i { print $j}' i=$i j=$j /root/test.txt) #本条对应内存
let "j++"
html_dis=$(awk 'NR==i { print $j}' i=$i j=$j /root/test.txt) #本条对应磁盘

if [ $html_cpu -gt 80 ];then
html_input_red $html_host $html_cpu $html_mem $html_dis #构造每行表格信息
else
html_input $html_host $html_cpu $html_mem $html_dis #构造每行表格信息
fi

let "i++"
echo $html_host $html_cpu $html_mem $html_dis $i $j
done
echo "</table>" >> /root/mail.html
} # 制作mail.html,并使用邮件发送

set_info

EMAIL_EXCEL=$(cat /root/mail.html)


sendEmail -f ${EMAIL_SENDER} -t ${EMAIL_RECIVER} -s ${EMAIL_SMTPHOST} -u ${EMAIL_TITLE} -xu ${EMAIL_USERNAME} -xp ${EMAIL_PASSWORD} -m ${EMAIL_EXCEL} ${EMAIL_CONTENT} -a ${FILE1_PATH} -o message-charset=utf-8 -o message-content-type=html
[root@hadoop ~]#

结果:

1
2
3
4
5
[root@hadoop ~]# ./test.sh 
192.168.232.8 90 95 10 2 4
192.168.232.9 20 40 40 3 4
192.168.232.10 80 50 40 4 4
Apr 12 17:12:21 hadoop sendEmail[12064]: Email was sent successfully!

mail.html文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<table border=1>
<tr>
<th>主机ip</th>
<th>cpu使用率</th>
<th>内存使用率</th>
<th>磁盘使用率</th>
</tr>
<tr bgcolor=#FF0000>
<td>192.168.232.8</td>
<td>90</td>
<td>95</td>
<td>10</td>
</tr>
<tr>
<td>192.168.232.9</td>
<td>20</td>
<td>40</td>
<td>40</td>
</tr>
<tr>
<td>192.168.232.10</td>
<td>80</td>
<td>50</td>
<td>40</td>
</tr>
</table>